const resolve = require('path').resolve
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const url = require('url')
const publicPath = ''

module.exports = (options = {}) => ({
  entry: {
    // vendor: './src/vendor.js',
    index: './src/main.js'
  },
  output: {
    path: resolve(__dirname, 'dist'),
    filename: options.dev ? '[name].js' : '[name].js?[chunkhash]',
    chunkFilename: '[id].js?[chunkhash]',
    publicPath: options.dev ? '/assets/' : publicPath
  },
  module: {
    rules: [{
        test: /\.vue$/,
        use: [{
          loader: 'vue-loader',
          options: {
            extractCSS: true,
            transformToRequire: {
              img: 'src',
              image: 'xlink:href',
              video: 'src',
              source: 'src'
            }
          }
        }],

      },
      {
        test: /\.js$/,
        use: ['babel-loader'],
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader', 'postcss-loader']
      },
      {
        test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz|mp4)(\?.+)?$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 1024
          }
        }]
      },
    ]
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      names: ['vendor', 'manifest']
    }),
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ],
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      '~': resolve(__dirname, 'src'),
      '@': resolve(__dirname, 'node_modules'),
    }
  },
  devServer: {
    host: '127.0.0.1',
    port: 8010,
    proxy: {
      '/api/': {
        target: 'http://test.rcbao-online.com/api/',
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/uploadfiles/': {
        target: 'http://test.rcbao-online.com/uploadfiles/',
        changeOrigin: true,
        pathRewrite: {
          '^/uploadfiles': ''
        }
      }
    },
    historyApiFallback: {
      index: url.parse(options.dev ? '/assets/' : publicPath).pathname
    }
  },
  externals: {
    'vue': 'Vue',
    'vue-router': 'VueRouter',
    'vuex': 'Vuex',
    'axios': 'axios'
  },
  devtool: options.dev ? 'eval-source-map' : 'source-map'
  // devtool: '#eval-source-map'
})
